import http from '@/utils/http'

const BASE_URL = '/system/sys-api'

export interface DataRecord {
  id?: number
  handle?: string
  title?: string
  path?: string
  type?: string
  action?: string
  createTime?: string
}

export interface ListParam {
  title?: string
  type?: string
  page?: number
  size?: number
  sort?: Array<string>
}

export interface ListRes {
  list: DataRecord[]
  total: number
}

export interface TreeNodeData {
  key?: string | number
  title?: string
  selectable?: boolean
  disabled?: boolean
  disableCheckbox?: boolean
  checkable?: boolean
  draggable?: boolean
  isLeaf?: boolean
  icon?: () => VNode
  switcherIcon?: () => VNode
  loadingIcon?: () => VNode
  dragIcon?: () => VNode
  children?: TreeNodeData[]
}
/**
 * 获取API列表
 */
export function listApi(query: ListParam) {
  return http.get<ListRes>(`${BASE_URL}`, query)
}
/** 查询单个api */
export function getApi(id: string) {
  return http.get<DataRecord>(`${BASE_URL}/${id}`)
}
/** 添加Api */
export function addApi(data: DataRecord) {
  return http.post(`${BASE_URL}`, data)
}
/**
 * 更新Api
 * @param data  更新数据
 */
export function updateApi(data: DataRecord, id: number) {
  return http.put(`${BASE_URL}/${id}`, data)
}
/**
 * 删除api
 * @param ids 主键列表
 * @returns
 */
export function deleteApi(ids: number | string | Array<string>) {
  return http.del(`${BASE_URL}/${ids}`)
}
/** @desc 导出json */
export function exportApi(query: any) {
  return http.download(`${BASE_URL}/export`, query)
}
/**
 * 获取API分类树结构
 */
export function listCategory(api?: any) {
  return http.get<TreeNodeData[]>(`${BASE_URL}/category`, { isApi: api })
}

/** 添加Api */
export function addApiCategory(data: TreeNodeData) {
  return http.post(`${BASE_URL}/category`, data)
}
/**
 * 更新Api
 * @param data  更新数据
 */
export function updateApiCategory(data: TreeNodeData, id: number | string) {
  return http.put(`${BASE_URL}/category/${id}`, data)
}
/**
 * 删除api
 * @param ids 主键列表
 * @returns
 */
export function deleteApiCategory(id: string | number) {
  return http.del(`${BASE_URL}/category/${id}`)
}
